package controller;

import java.io.File;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import entity.Comment;
import service.BlogService;
import service.CommentService;

@Controller
public class CommentController {
	@Resource(name = "blogServiceImpl")
	private BlogService blogService;

	@Resource(name = "commentServiceImpl")
	private CommentService commentService;

	@RequestMapping("add_comment")
	public ModelAndView addComment(HttpServletRequest request) {
		int essayId = Integer.parseInt(request.getParameter("essayId"));

		saveComment(request, essayId, 0);

		return new ModelAndView("redirect:comment?id=" + essayId);

	}

	@ResponseBody
	@RequestMapping(path = "comment_data", produces = "application/json;charset=UTF-8")
	public String commentData(HttpServletRequest req) {
		int id = Integer.parseInt(req.getParameter("id"));

		List<Comment> comments = commentService.findCommentsByEssayId(id);

		JSONArray jsonArray = new JSONArray();
		for (Comment c : comments) {
			JSONObject jobj = new JSONObject();
			jobj.put("id", c.getId());
			jobj.put("essay_id", c.getEssay_id());
			jobj.put("time", c.getTime());
			jobj.put("review", c.getReview());
			jobj.put("reply_id", c.getReply_id());
			jobj.put("username", c.getUsername());
			jobj.put("email", c.getEmail());
			jobj.put("head", c.getHead());
			jsonArray.put(jobj);
		}
		return jsonArray.toString();
	}

	@RequestMapping("comment")
	public ModelAndView commentReq(HttpServletRequest req) {
		ModelAndView mv = new ModelAndView("comment");
		int id = Integer.parseInt(req.getParameter("id"));
		mv.addObject("essay", blogService.getById(id));
		//根据文章的先后顺序改变
		mv.addObject("next", blogService.findPreviousId(id));
		mv.addObject("previous", blogService.findNextId(id));
		return mv;
	}
	
	
	public BlogService getBlogService() {
		return blogService;
	}

	public CommentService getCommentService() {
		return commentService;
	}

	/**
	 * 获取img/face目录下的图片名称
	 * 
	 * @param request
	 * @return List
	 */
	private List<String> getImageNames(HttpServletRequest request) {
		List<String> imgNames = new ArrayList<String>();
		String facePath = request.getSession().getServletContext().getRealPath("/img/face");
		File dirPath = new File(facePath);

		File[] files = dirPath.listFiles();
		for (File f : files) {
			imgNames.add(f.getName());
		}
		return imgNames;
	}

	@RequestMapping("reply_comment")
	public ModelAndView replyComment(HttpServletRequest request) {
		int essayId = Integer.parseInt(request.getParameter("essayId"));
		int replyId = Integer.parseInt(request.getParameter("replyId"));
		saveComment(request, essayId, replyId);
		return new ModelAndView("redirect:comment?id=" + essayId);
	}

	private void saveComment(HttpServletRequest request, int essayId, int replyId) {
		String name = request.getParameter("username");
		String email = request.getParameter("email");
		String review = request.getParameter("textarea");
		Comment comment = new Comment();
		comment.setEmail(email);
		comment.setEssay_id(essayId);
		List<String> heads = getImageNames(request);
		comment.setHead(heads.get(new Random().nextInt(heads.size())));
		comment.setReply_id(replyId);
		comment.setReview(review);
		comment.setTime(new Timestamp(new Date().getTime()));
		comment.setUsername(name);

		commentService.addComment(comment);
	}

	public void setBlogService(BlogService blogService) {
		this.blogService = blogService;
	}

	public void setCommentService(CommentService commentService) {
		this.commentService = commentService;
	}
}
